package com.py.credit.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.Dynamicnews;
import com.py.credit.entity.SysUser;
import com.py.credit.service.DynamicnewsService;
import com.py.credit.service.RemoteBaseService;
import com.py.framework.core.annotation.IgnoreAuthority;
import com.py.framework.core.common.Page;
import com.py.framework.core.helper.HttpServletHelper;

@RestController
@RequestMapping("/Dynamicnews")
public class DynamicnewsController {

	@Autowired
	private DynamicnewsService dynamicnewsService;

	@Autowired
	private RemoteBaseService remoteBaseService;

	@Value("${imageurl}")
	private String imageurl;

	@Value("${imagepath}")
	private String imagepath;

	@RequestMapping("/picupload.do")
	public RestServiceResult<Object> pic(@RequestParam("file") MultipartFile file) throws IOException {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		InputStream input = file.getInputStream();
		String originalFilename = file.getOriginalFilename();
		String subfix = originalFilename.substring(originalFilename.lastIndexOf("."));
		String picName = System.currentTimeMillis() + subfix;
		String path = imagepath + File.separator + picName;
		String picurl = imageurl + File.separator + picName;
		FileOutputStream fout = new FileOutputStream(path);
		byte[] b = new byte[1024];
		int length = 0;
		while ((length = input.read(b)) > 0) {
			fout.write(b, 0, length);
		}
		input.close();
		fout.close();
		result.setData(picurl);
		return result;
	}

	@RequestMapping(value = "/addDynamicnews.json")
	public RestServiceResult<Object> addDynamicnews(
			@RequestParam(value = "title", defaultValue = "", required = false) String title,
			@RequestParam(value = "content", defaultValue = "", required = false) String content,
			@RequestParam(value = "case_type", defaultValue = "", required = false) String case_type,
			@RequestParam(value = "introduce", defaultValue = "", required = false) String introduce,
			@RequestParam(value = "release_source", defaultValue = "", required = false) String release_source,
			@RequestParam(value = "iscolumn", defaultValue = "", required = false) String iscolumn,
			@RequestParam(value = "release_status", defaultValue = "", required = false) String release_status,
			@RequestParam(value = "pic_path", defaultValue = "", required = false) String pic_path,
			@RequestParam(value = "release_type", defaultValue = "", required = false) String release_type,
			HttpServletRequest request) {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		Dynamicnews dynamicnews = new Dynamicnews();
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser usrInfo = new SysUser();
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			usrInfo = user.getData();
		}
		dynamicnews.setTitle(title);
		dynamicnews.setContent(content);
		dynamicnews.setCase_type(case_type);
		dynamicnews.setIntroduce(introduce);
		dynamicnews.setRelease_source(release_source);
		dynamicnews.setIscolumn(iscolumn);
		dynamicnews.setRelease_status(release_status);
		dynamicnews.setPic_path(pic_path);
		dynamicnews.setRelease_type(release_type);
		dynamicnews.setUserName(usrInfo.getUsername());
		try {
			dynamicnewsService.addDynamicnews(dynamicnews);
			result.setMsg("添加成功");
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("添加失败");
		}
		return result;
	}

	@RequestMapping("/deleteDynamicnewsById.json")
	public RestServiceResult<Object> deleteDynamicnewsById(
			@RequestParam(value = "ids", defaultValue = "", required = false) String ids) {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		List<String> idList = java.util.Arrays.asList(ids.split(","));
	
			
		try {
			for(String id:idList) {
			dynamicnewsService.deleteDynamicnewsById(Integer.parseInt(id));
			}
			result.setMsg("删除成功");
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("删除失败");
		}
		return result;
	}

	@RequestMapping("/updateDynamicnewsById.json")
	public RestServiceResult<Object> updateDynamicnewsById(
			@RequestParam(value = "id", defaultValue = "", required = false) Integer id,
			@RequestParam(value = "title", defaultValue = "", required = false) String title,
			@RequestParam(value = "content", defaultValue = "", required = false) String content,
			@RequestParam(value = "introduce", defaultValue = "", required = false) String introduce,
			@RequestParam(value = "release_source", defaultValue = "", required = false) String release_source,
			@RequestParam(value = "iscolumn", defaultValue = "", required = false) String iscolumn,
			@RequestParam(value = "release_status", defaultValue = "", required = false) String release_status,
			@RequestParam(value = "pic_path", defaultValue = "", required = false) String pic_path,
			@RequestParam(value = "case_type", defaultValue = "", required = false) String case_type,
			HttpServletRequest request) {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		Dynamicnews dynamicnews = new Dynamicnews();
		dynamicnews.setId(id);
		dynamicnews.setTitle(title);
		dynamicnews.setContent(content);
		dynamicnews.setIntroduce(introduce);
		dynamicnews.setRelease_source(release_source);
		dynamicnews.setIscolumn(iscolumn);
		dynamicnews.setRelease_status(release_status);
		dynamicnews.setPic_path(pic_path);
		dynamicnews.setCase_type(case_type);
		try {
			dynamicnewsService.updateDynamicnewsById(dynamicnews);
			result.setMsg("修改成功");
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("修改失败");
		}
		return result;
	}

	@RequestMapping("/selectDynamicnewsById.json")
	public RestServiceResult<Dynamicnews> selectDynamicnewsById(
			@RequestParam(value = "id", defaultValue = "", required = false) Integer id) {
		RestServiceResult<Dynamicnews> result = new RestServiceResult<Dynamicnews>();
		Dynamicnews dynamicnews = dynamicnewsService.selectDynamicnewsById(id);
		result.setData(dynamicnews);
		return result;
	}
	@IgnoreAuthority
	@RequestMapping("/selectDynamicnewsByLikeForPage.json")
	public RestServiceResult<List<Dynamicnews>> selectDynamicnewsByLikeForPage(HttpServletRequest request,
			@RequestParam(value = "release_type", defaultValue = "", required = false) String release_type,
			@RequestParam(value = "release_status", defaultValue = "", required = false) String release_status,
			@RequestParam(value = "title", defaultValue = "", required = false) String title,
			@RequestParam(value = "content", defaultValue = "", required = false) String content,
			@RequestParam(value = "release_source", defaultValue = "", required = false) String release_source,
			@RequestParam(value = "case_type", defaultValue = "", required = false) String case_type,
			@RequestParam(value = "iscolumn", defaultValue = "", required = false) String iscolumn, Page page) {
		RestServiceResult<List<Dynamicnews>> result = new RestServiceResult<List<Dynamicnews>>();
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		 String rolename= "";
		 Integer userType = null;
		 SysUser userInfo =null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			 userInfo = user.getData();
	
			if (null != userInfo) {
				rolename = userInfo.getRoleName();
				userType = userInfo.getUserType();
			}else {
				ExceptionLogger.error("BatchDataController#dataList", "未获取到当前用户信息！");
			}
			}else {
				ExceptionLogger.error("BatchDataController#dataList", "未获取到当前用户信息！");
			}
		Dynamicnews dynamicnews = new Dynamicnews();
		if(userInfo.getOfficeId()==0) {
			dynamicnews.setRelease_status("Q");
			dynamicnews.setRelease_type(release_type);
			dynamicnews.setTitle(title);
			
			dynamicnews.setContent(content);
			dynamicnews.setRelease_source(release_source);
			dynamicnews.setCase_type(case_type);
			dynamicnews.setIscolumn(iscolumn);
			List<Dynamicnews> data = dynamicnewsService.selectDynamicnewsByLikeForPage(dynamicnews, page);
			result.setData(data);
			result.setCount(page.getTotalCount());
			return result;
		}
		if(!rolename.equals("系统管理员")) {
			dynamicnews.setRelease_status("Q");
		}else {
			dynamicnews.setRelease_status("");
		}
		
		dynamicnews.setRelease_type(release_type);
		dynamicnews.setTitle(title);
		
		dynamicnews.setContent(content);
		dynamicnews.setRelease_source(release_source);
		dynamicnews.setCase_type(case_type);
		dynamicnews.setIscolumn(iscolumn);
		try {
			List<Dynamicnews> data = dynamicnewsService.selectDynamicnewsByLikeForPage(dynamicnews, page);
			result.setData(data);
			result.setCount(page.getTotalCount());
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("查询失败");
		}
		return result;
	}

	@RequestMapping("/selectOneById.json")
	public RestServiceResult<Dynamicnews> selectOneById(
			@RequestParam(value = "id", defaultValue = "", required = false) Integer id) {
		RestServiceResult<Dynamicnews> result = new RestServiceResult<Dynamicnews>();
		try {
			Dynamicnews dynamicnews = dynamicnewsService.selectDynamicnewsById(id);
			result.setData(dynamicnews);
			int count = dynamicnews.getCount();
			count = count + 1;
			dynamicnews.setCount(count);
			dynamicnews.setId(id);
			dynamicnewsService.updateDynamicnewsCount(dynamicnews);
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("访问计数失败");
		}
		return result;
	}

}
